{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9e34398d-9469-400a-84ad-2fba49e45ee9",
   "metadata": {},
   "source": [
    "## 函数与文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4ec58d33-c939-4a2d-8470-6752bd6b09ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<svg width=\"300\" height=\"150\" style=\"fill:none; stroke-linecap:round;\">\n",
       "    <rect width=\"100%\" height=\"100%\" fill=\"#F3F3F7\" />\n",
       "\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 150,75\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 100,100 150,100\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 100,100 100,50 70.6,90.5 118.2,75 70.6,59.5 100,100\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 100,100 100,50 70.6,90.5 118.2,75 70.6,59.5 100,100\" />'\n",
       "\n",
       "<g transform=\"rotate(180.0,100.0,100.0) translate(100.0, 100.0)\">\n",
       "    <circle stroke=\"#63A375\" stroke-width=\"2\" fill=\"transparent\" r=\"5.5\" cx=\"0\" cy=\"0\"/>\n",
       "    <polygon points=\"0,12 2,9 -2,9\" style=\"fill:#63A375;stroke:#63A375;stroke-width:2\"/>\n",
       "</g>\n",
       "\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "ename": "NameError",
     "evalue": "name 'height' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 12\u001b[0m\n\u001b[0;32m      9\u001b[0m             t\u001b[38;5;241m.\u001b[39mfd(height)\n\u001b[0;32m     10\u001b[0m         t\u001b[38;5;241m.\u001b[39mlt(\u001b[38;5;241m90\u001b[39m)\n\u001b[1;32m---> 12\u001b[0m rectangle(x\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m100\u001b[39m,y\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m100\u001b[39m,width\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m50\u001b[39m,heigh\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m30\u001b[39m)\n",
      "Cell \u001b[1;32mIn[1], line 9\u001b[0m, in \u001b[0;36mrectangle\u001b[1;34m(x, y, width, heigh)\u001b[0m\n\u001b[0;32m      7\u001b[0m     t\u001b[38;5;241m.\u001b[39mfd(width)\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m----> 9\u001b[0m     t\u001b[38;5;241m.\u001b[39mfd(height)\n\u001b[0;32m     10\u001b[0m t\u001b[38;5;241m.\u001b[39mlt(\u001b[38;5;241m90\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'height' is not defined"
     ]
    }
   ],
   "source": [
    "import jupyturtle as t \n",
    "# t.make_turtle(animate=True,delay=0.02, width=400, height=400)\n",
    "def rectangle(x, y, width, heigh):\n",
    "    t.jump_to(x, y)\n",
    "    for i in range(4):\n",
    "        if i ==0 or i ==2:\n",
    "            t.fd(width)\n",
    "        else:\n",
    "            t.fd(height)\n",
    "        t.lt(90)\n",
    "        \n",
    "rectangle(x=100,y=100,width=50,heigh=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0452f548-3020-4cf2-95e0-136aa2fcc4ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<svg width=\"300\" height=\"150\" style=\"fill:none; stroke-linecap:round;\">\n",
       "    <rect width=\"100%\" height=\"100%\" fill=\"#F3F3F7\" />\n",
       "\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 150,75\" />'\n",
       "\n",
       "<path stroke=\"#663399\" stroke-width=\"2\" d=\"M 100,100 150,100 109.5,70.6 125,118.2 140.5,70.6 100,100\" />'\n",
       "\n",
       "<g transform=\"rotate(-90.0,100.0,100.0) translate(100.0, 100.0)\">\n",
       "    <circle stroke=\"#63A375\" stroke-width=\"2\" fill=\"transparent\" r=\"5.5\" cx=\"0\" cy=\"0\"/>\n",
       "    <polygon points=\"0,12 2,9 -2,9\" style=\"fill:#63A375;stroke:#63A375;stroke-width:2\"/>\n",
       "</g>\n",
       "\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import jupyturtle as t\n",
    "\n",
    "def pentagram(x,y,width):\n",
    "\n",
    "    t.jump_to(x,y)\n",
    "    for i in range(5):\n",
    "        t.fd(50)\n",
    "        t.lt(144)\n",
    "pentagram(x=100,y=100,width=50)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "34ffc863-3aca-4015-a470-db90b09023c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5773502572807828\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "print(math.tan(30/180.*3.1415926))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "67c0e0e3-80f8-4543-8fad-0812f81eb427",
   "metadata": {},
   "outputs": [],
   "source": [
    "def pentagram1(x,y,r):\n",
    "    t.jump_to(x-r*math.cos(18/180*math.pi),y-r*math.sin(18/180*math.pi))\n",
    "    for i in range(5):\n",
    "        t.fd(2*r*math.cos(18/180*math.pi))\n",
    "        t.rt(144)\n",
    "    t.lt(72)\n",
    "    t.jump_to(x+r*math.cos(18/180*math.pi),y+r*math.sin(18/180*math.pi))\n",
    "    t.jump_to(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "661e4563-6f0c-4826-a9f1-a553901131dc",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "incomplete input (402949741.py, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[12], line 4\u001b[1;36m\u001b[0m\n\u001b[1;33m    \u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m incomplete input\n"
     ]
    }
   ],
   "source": [
    "import jupyturtle as t \n",
    "import math\n",
    "def pentagram1(x,y,r):\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "592e396f-4058-474c-a06c-6067e8aecc6b",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "expected an indented block (2032390170.py, line 5)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\sw\\AppData\\Local\\Temp\\ipykernel_13468\\2032390170.py\"\u001b[1;36m, line \u001b[1;32m5\u001b[0m\n\u001b[1;33m    \u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
     ]
    }
   ],
   "source": [
    "def pentagram1(x,y,r):\n",
    "    \n",
    "    \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37820a0e-db7f-4bf9-9134-5d197b81a730",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 读文件(File)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fc813037-409d-463e-86c6-2e860818f214",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "75.07，74.74，73.89，51.31，55.19，16.58，76.31，77.81，32.34，99.95，\n",
      "27.69，13.38，47.21，23.45，56.36，81.75，28.49，83.89，70.53，78.56，\n",
      "69.71，67.03，85.95，68.18，91.64，53.95，29.91，84.53，66.42，72.51，\n",
      "62.68，11.20，70.21，41.60，99.48，11.68，66.98，53.75，27.75，67.49，\n",
      "90.60，19.90，19.98，43.66，81.45，90.65，33.81，75.17，79.49，90.82，\n",
      "17.80，24.13，96.23，84.12，72.13，99.18，17.14，63.63，39.86，61.74，\n",
      "63.96，82.72，31.11，78.71，53.45，43.84，33.50，48.17，26.60，69.58，\n",
      "52.24，45.35，77.65，37.96，51.35，28.56，40.42， 3.34，40.35，65.01，\n",
      "55.96，90.26，81.96，91.09，17.33，27.87，29.33， 9.53，73.02，10.09，\n",
      "81.21，79.96，34.58，77.39，61.14，51.62，83.47，20.57，90.13，85.30，\n",
      "\n"
     ]
    }
   ],
   "source": [
    "file = open('test.txt','r')#open函数\n",
    "content= file.read()       #读取内容\n",
    "file.close()               #关闭文件\n",
    "print(content)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "11ffe011-bc79-4fbb-818d-d3b3e957e9b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "75.07，74.74，73.89，51.31，55.19，16.58，76.31，77.81，32.34，99.95，\n",
      "\n",
      "27.69，13.38，47.21，23.45，56.36，81.75，28.49，83.89，70.53，78.56，\n",
      "\n",
      "69.71，67.03，85.95，68.18，91.64，53.95，29.91，84.53，66.42，72.51，\n",
      "\n",
      "62.68，11.20，70.21，41.60，99.48，11.68，66.98，53.75，27.75，67.49，\n",
      "\n",
      "90.60，19.90，19.98，43.66，81.45，90.65，33.81，75.17，79.49，90.82，\n",
      "\n",
      "17.80，24.13，96.23，84.12，72.13，99.18，17.14，63.63，39.86，61.74，\n",
      "\n",
      "63.96，82.72，31.11，78.71，53.45，43.84，33.50，48.17，26.60，69.58，\n",
      "\n",
      "52.24，45.35，77.65，37.96，51.35，28.56，40.42， 3.34，40.35，65.01，\n",
      "\n",
      "55.96，90.26，81.96，91.09，17.33，27.87，29.33， 9.53，73.02，10.09，\n",
      "\n",
      "81.21，79.96，34.58，77.39，61.14，51.62，83.47，20.57，90.13，85.30，\n",
      "\n",
      "27.69，13.38，47.21，23.45，56.36，81.75，28.49，83.89，70.53，78.56，\n",
      "\n"
     ]
    }
   ],
   "source": [
    "file = open('test.txt','r')#open函数\n",
    "lines= file.readlines()     #\n",
    "file.close()               #关闭文件\n",
    "for line in lines:         #可以把文本中的字符拿出来变成列表\n",
    "    print(line)\n",
    "print(lines[1])           #读取第一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3b79c802-ff9d-4687-a46e-df2a693f50bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['75.07，74.74，73.89，51.31，55.19，16.58，76.31，77.81，32.34，99.95，\\n27.69，13.38，47.21，23.45，56.36，81.75，28.49，83.89，70.53，78.56，\\n69.71，67.03，85.95，68.18，91.64，53.95，29.91，84.53，66.42，72.51，\\n62.68，11.20，70.21，41.60，99.48，11.68，66.98，53.75，27.75，67.49，\\n90.60，19.90，19.98，43.66，81.45，90.65，33.81，75.17，79.49，90.82，\\n17.80，24.13，96.23，84.12，72.13，99.18，17.14，63.63，39.86，61.74，\\n63.96，82.72，31.11，78.71，53.45，43.84，33.50，48.17，26.60，69.58，\\n52.24，45.35，77.65，37.96，51.35，28.56，40.42， 3.34，40.35，65.01，\\n55.96，90.26，81.96，91.09，17.33，27.87，29.33， 9.53，73.02，10.09，\\n81.21，79.96，34.58，77.39，61.14，51.62，83.47，20.57，90.13，85.30，\\n']\n"
     ]
    }
   ],
   "source": [
    "with open('test.txt','r') as file:  #用with可以不需要关闭文件\n",
    "    content = file.read()\n",
    "print(content.split(','))          #用“,”分开字符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0df7bcc1-118c-47fe-837a-f4add44619ed",
   "metadata": {},
   "source": [
    "## 写文件(write)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "48a049e5-cb64-4d12-9285-d1bd227f095f",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "expected ':' (3629372830.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[7], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    with open('test.txt' , 'w') #删掉所有文本内容重新写\u001b[0m\n\u001b[1;37m                                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m expected ':'\n"
     ]
    }
   ],
   "source": [
    "with open('test.txt' , 'w') #删掉所有文本内容重新写\n",
    "with open('test.txt' , 'a') #在原来文本后面接着写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "288900fb-3d36-454f-891f-bd20c2fa2b5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  90.859  99.015  59.287  64.620  17.217  73.426  28.518  15.764  30.379  73.945\n",
      "  26.766  36.236  45.901  30.190  41.050  13.548  27.944  40.349   2.609  44.075\n",
      "  39.726  63.422  24.376  15.780  35.226  63.466  44.312  63.554  32.961  30.199\n",
      "  58.365  44.033  61.682  65.891  65.774   3.506  64.767  68.202  99.569  14.601\n",
      "   7.681  58.945  86.087  78.883  33.350  60.868   4.490  40.170  18.184   4.638\n",
      "  92.436  44.907  51.361  56.547  26.927  52.850  78.638  28.605  11.719  51.800\n",
      "  71.660  94.968  35.878  56.843  39.825  71.352  44.734  90.956  93.988  99.123\n",
      "  99.261  81.439   8.806  87.932  23.405  24.633  54.329   3.215  80.245  61.828\n",
      "  40.311  24.126  60.276  61.812  11.654  80.412  22.522  83.960  72.518  89.054\n",
      "  66.496  23.239  45.021  97.613  36.892  12.072  67.941  46.252  56.209  54.511\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "a =[]\n",
    "for i in range(100):\n",
    "    print('%8.3f' % (random.random()*100), end='')\n",
    "    if ( (i+1) %10 ==0):\n",
    "        print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "50483edf-a089-47f4-b07a-05eee9109a79",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import random\n",
    "a = []\n",
    "for i in range(100):\n",
    "    number = random.randint(1,100)\n",
    "    a.append(number)\n",
    "with open('test.txt','w') as file:\n",
    "    for number in a:\n",
    "        file.write(f'{number}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "64318704-80bb-44d1-afa1-74a38646d571",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "75.07，74.74，73.89，51.31，55.19，16.58，76.31，77.81，32.34，99.95，\n",
      "27.69，13.38，47.21，23.45，56.36，81.75，28.49，83.89，70.53，78.56，\n",
      "69.71，67.03，85.95，68.18，91.64，53.95，29.91，84.53，66.42，72.51，\n",
      "62.68，11.20，70.21，41.60，99.48，11.68，66.98，53.75，27.75，67.49，\n",
      "90.60，19.90，19.98，43.66，81.45，90.65，33.81，75.17，79.49，90.82，\n",
      "17.80，24.13，96.23，84.12，72.13，99.18，17.14，63.63，39.86，61.74，\n",
      "63.96，82.72，31.11，78.71，53.45，43.84，33.50，48.17，26.60，69.58，\n",
      "52.24，45.35，77.65，37.96，51.35，28.56，40.42， 3.34，40.35，65.01，\n",
      "55.96，90.26，81.96，91.09，17.33，27.87，29.33， 9.53，73.02，10.09，\n",
      "81.21，79.96，34.58，77.39，61.14，51.62，83.47，20.57，90.13，85.30，\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "s =''\n",
    "for i in range(100):\n",
    "    num = random.random()*100\n",
    "    s = s+f'{num:5.2f}'+'，'\n",
    "    if (i+1)%10 ==0:\n",
    "        s = s + \"\\n\"\n",
    "print(s)\n",
    "with open(\"test.txt\",\"w\") as file:\n",
    "    file.write(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16da66c9-8f1d-42b4-a550-6b68ae585660",
   "metadata": {},
   "source": [
    "## 作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9870f59-3907-4c9a-9478-d7bd9e5c6724",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "13cd66fa-bbfa-45dd-8911-3372ad721d3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle\n",
    "import math\n",
    "\n",
    "\n",
    "screen = turtle.Screen()       #创建画布\n",
    "star_turtle = turtle.Turtle()  \n",
    "star_turtle.hideturtle()       # 隐藏海龟\n",
    "\n",
    "\n",
    "def draw_star(center_x, center_y, radius):  #设置函数参数\n",
    "    star_turtle.penup()\n",
    "    star_turtle.goto(center_x, center_y)\n",
    "    star_turtle.pendown()\n",
    "    for _ in range(5):                      #循环五次来画五角星的顶点\n",
    "        star_turtle.forward(radius//2)      #移动半个个半径的长度，不然会重叠\n",
    "        star_turtle.right(144)              #旋转144度\n",
    "\n",
    "\n",
    "angle = 360 / 12                            # 十二颗星星之间的角度\n",
    "radius = 50                                 # 设置星星的半径\n",
    "\n",
    "for i in range(12):\n",
    "    x = 0 + radius * math.cos(math.radians(angle * i))#几何关系求出星星的中心\n",
    "    y = 0 + radius * math.sin(math.radians(angle * i))\n",
    "    draw_star(x, y, radius)\n",
    "\n",
    "star_turtle.hideturtle()\n",
    "turtle.done()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a36c55c-0db1-4cc0-bc0f-94027eb9e645",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32632905-ff51-4a8c-9631-78a80f8fe7bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "\n",
    "random_numbers = []       # 创建一个空列表来存储随机数\n",
    "\n",
    "\n",
    "for _ in range(100):       # 生成100个1到100之间的随机数\n",
    "    number = random.randint(1, 100)\n",
    "    random_numbers.append(number)\n",
    "\n",
    "\n",
    "with open('random_numbers.txt', 'w') as file:# 将随机数写入文件\n",
    "    for i, number in enumerate(random_numbers):#每10个数字后换行\n",
    "        file.write(f\"{number}\")\n",
    "        if (i + 1) % 10 != 0:\n",
    "            file.write(\" \")\n",
    "        else:\n",
    "            file.write(\"\\n\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bed3a96e-6960-4d5e-b1a7-b81645676050",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0-10: 17\n",
      "10-20: 12\n",
      "20-30: 12\n",
      "30-40: 6\n",
      "40-50: 11\n",
      "50-60: 9\n",
      "60-70: 10\n",
      "70-80: 11\n",
      "80-90: 7\n",
      "90-100: 5\n"
     ]
    }
   ],
   "source": [
    "\n",
    "with open('random_numbers.txt', 'r') as file:# 打开文件并读取内容\n",
    "    content = file.read()                    #提取内容\n",
    "\n",
    "\n",
    "numbers = list(map(int, content.split()))    # 将内容拆分成数字列表，用map转换为整数\n",
    "\n",
    "\n",
    "count = [0] * 10#创建十个只有0的列表\n",
    "\n",
    "for number in numbers:                       # 统计各个分数段的数据个数\n",
    "    segment = (number // 10) - 1              #把十位的数字换成个位数\n",
    "    \n",
    "    count[segment] += 1                      #把1补回来\n",
    "\n",
    "\n",
    "for i in range(10):\n",
    "    start = i * 10\n",
    "    end = (i + 1) * 10\n",
    "    print(f\"{start}-{end}: {count[i]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1605e82b-f2dd-4850-8e15-e02167e144e0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
